
(require (lib "scheme/host/keyword.scm" "rsc"))

;; Store the SynthDef to be used in the score.

(synthdef-send
 (synthdef
  "sine"
  ((freq 440))
  (Out.ar 0 (Pan2.ar (Mul (SinOsc.ar freq 0 0.2) 
			  (Line.kr 1 0 0.5 doneAction: 2))
		     (IRand.ir -1 1)))))

;; A procedure to generate a score.

(defineK (make-score (n 20))
  (define (get-note)
    (midi-note->frequency (randomC '(60 62 64 65 67 69 71 72))))
  (cons (list 0 (list "/g_new" 1 0 0))
	(map (lambda (t) 
	       (list t (list "/s_new" "sine" -1 1 1 "freq" (get-note))))
	     (iota n 0 0.1))))

;; Generate a score and write to an OSC file. 

(score-write-osc-file (make-score) "/tmp/test.osc")

;; Read the OSC file from disk, the result is a score.

(score-read-osc-file "/tmp/test.osc")

;; Play the stored score using the current-server.

(score-play (score-read-osc-file "/tmp/test.osc"))

;; Play a newly generated score.

(score-play (make-score 128))

;; Generate the command to render a score file to an audio file.

(score-render-command
 "/tmp/test.osc" "/tmp/test.aif" 
 output-channels: 1
 scsynth: (server-command-name))

;; Generate an OSC file and request the host to render that to an
;; audio file.

(score-render 
 (make-score) "/tmp/test.osc" "/tmp/test.aif" 
 output-channels: 1
 scsynth: (server-command-name))

;; Render an existing OSC score file.

(score-render-osc-file
 "/tmp/test.osc" "/tmp/test.aif" 
 output-channels: 1)

;; Examine result.

(system "open /tmp/test.aif")
